#Include ..\baseHandle.ahk

class DynamicRun extends BaseHandle {

  static Handle(parsed) {
    target := parsed.target
    mcf := MeowConf.Of('./cfg/misc/dr.txt')
    if !mcf.Has(target)
      return this.Fail('未配置')
    str := _format(mcf.Get(target), parsed.extra)
    Run(A_ComSpec ' /c ' str, , 'hide')
    return this.Succ('ok.', 'x')

    _format(str, vals) {
      _pos := 1, idx := [], post := ''
      for i, v in str {
        if v = '{' {
          _pos := i
        } else if v = '}' {
          idx.Push(str.substring(_pos + 1, i))
          post := SubStr(post, 1, post.Length - i + _pos + 1)
        }
        post .= v
      }
      for i, v in vals
        idx[i] := v
      return Format(post, idx*)
    }
  }

  static Spec() {
    return {
      cmd: 'dr',
      desc: '使用提供的参数运行配置项',
      format: 'dr <target> [extra]...',
      args: {
        desc: '要测试的配置文件路径',
        isOptional: false,
        cacheable: true,
        generator: {
          script: '',
          postProcess: (*) => MeowConf.Of('./cfg/misc/dr.txt').data.keys
        }
      },
      extra: {
        desc: '动态插入值',
        isEnable: true
      }
    }
  }

}